<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>申购单据查询</title>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/default/easyui.css"></link>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/icon.css"></link>
    <script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>

    <script type="text/javascript" src="/static/js/date.js"></script>
    <script src="/static/js/FileSaver.min.js"></script>
    <script src="/static/js/xlsx.full.min.js"></script>
    <script type="text/javascript">

        var url;
        //禁止整体该页面的右键弹出html相关,不阻碍easyui右键
        $(function () {
            $(document).bind('contextmenu', function (e) {
                e.preventDefault();
                $('#tree-0').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            });
        });

        //加载tree类别
        function openProjListChoose() {
            $("#projList").dialog("open").dialog("setTitle", "进入项目选择");

            $("#projTree").tree({
                url: '/admin/project/loadProjectTree',
                onLoadSuccess: function (node, data) {
                    $("#tree").tree('expandAll');
                },
                // onDblClick: function (node) {
                //
                // },
                //右键选择树形菜单操作
                onContextMenu: function (e, node) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    var rootNodes = $("#tree").tree('getRoots');
                    for (var i = 0; i < rootNodes.length; i++) {
                        var theNode = $("#tree").tree('find', rootNodes[i].id);
                        $("#tree").tree('uncheck', theNode.target);
                    }
                    $("#tree").tree('select', node.target);
                    if (node.attributes.state == 2) {
                        $("#tree-2").menu('show', {
                            left: e.pageX,//在鼠标点击处显示菜单
                            top: e.pageY
                        });
                    } else if (node.attributes.state == 1) {
                        $("#tree-1").menu('show', {
                            left: e.pageX,//在鼠标点击处显示菜单
                            top: e.pageY
                        });
                    } else {
                        $('#tree-0').menu('show', {
                            left: e.pageX,//在鼠标点击处显示菜单
                            top: e.pageY
                        });
                    }
                },
                //单击某类别 展示不同详细和按钮选择
                onClick: function (node) {
                    if (node.attributes.state == 2) { // 假如是二级菜单,可以删除
                        $("#projNameList").datagrid('load', {
                            "w_id": node.id
                        });
                    } else if (node.attributes.state == 1) {
                        $("#projNameList").datagrid('load', {
                            "o_id": node.id
                        });
                    }
                }
            });
            loadDataGrid();
        }

        //加载项目数据表格
        function loadDataGrid() {
            $("#projNameList").datagrid({
                //双击事件
                onDblClickRow: function (index, row) {
                    $("#projId").val(row.id);
                    $("#projName").val(row.xiangmumingcheng);
                    $("#projList").dialog("close");
                },
                onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    $(this).datagrid("clearSelections"); //取消所有选中项
                    $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                    $('#grid-data').menu('show', {
                        left: e.pageX,//在鼠标点击处显示菜单
                        top: e.pageY
                    });
                    e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                },
            });
        }

        //仓库列表选择
        function openStoreListChoose() {
            $("#storeList").dialog("open").dialog("setTitle", "进入仓库选择");
            $("#storeTree").tree({
                url: '',
                onLoadSuccess: function (node, data) {
                    $("#storeTree").tree('expandAll');
                },
                onClick: function (node) {
                    // if(node.attributes.state==0){ //如果是叶子节点,删除按钮恢复可用,只是展示时取消展示就可以了
                    //
                    // }
                    $("#dg5").datagrid('load', {
                        "id": node.id
                    });
                }
            });
        }

        // // 查询项目
        // function searchProj() {
        //     $("#dg4").datagrid('load', {
        //         "codeOrProjName": $("#s_codeOrProjName").val()
        //     })
        // }

        //查询库房
        function searchStore() {
            $("#dg5").datagrid('load', {
                "codeOrStoreName": $("#s_codeOrStoreName").val()
            })
        }

        //填充项目选择
        function saveProjChoose() {
            var node = $('#projTree').tree('getSelected'); // 获取选中节点
            $("#projId").val(node.id);
            $("#projName").val(node.text);
            $("#projList").dialog("close");
        }

        //填充仓库选择
        function saveStoreChoose() {
            var node = $('#storeTree').tree('getSelected');
            $("#storeId").val(node.id);
            $("#storeName").val(node.text);
            $("#storeList").dialog("close");
        }

        $(document).ready(function () {
            $("#s_btime").datebox("setValue", genLastMonthDayStr()); // 设置上个月日期
            $("#s_etime").datebox("setValue", genTodayStr()); // 设置当前日期
            //根据列举的申购单主表,点击其中每个,列举对应主表的详细数据
            $('#dg').datagrid({
                onClickRow: function (index, row) {
                    $("#dg2").datagrid({
                        url: '/admin/shengoudan/listDetailsByZId',
                        queryParams: {
                            zongzhangid: row.id
                        },
                        onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                            e.preventDefault(); //阻止浏览器捕获右键事件
                            $(this).datagrid("clearSelections"); //取消所有选中项
                            $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                            $('#grid-data').menu('show', {
                                left: e.pageX,//在鼠标点击处显示菜单
                                top: e.pageY
                            });
                            e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                        },
                    });
                },
                onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    $(this).datagrid("clearSelections"); //取消所有选中项
                    $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                    $('#grid-data').menu('show', {
                        left: e.pageX,//在鼠标点击处显示菜单
                        top: e.pageY
                    });
                    e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                },
            });

        });

        //搜索申购单
        function searchShengoudan() {
            $('#dg2').datagrid('loadData', {total: 0, rows: []});
            var projName = $("#projName").val();
            var xiangmumingcheng;
            if (projName.length == 0) {
                xiangmumingcheng = "";
            } else {
                xiangmumingcheng = projName;
            }
            console.log($("#s_danhao").val())
            var wuliaoshengoudanhao;
            var getdanhao = $("#s_danhao").val();
            if (getdanhao.length == 0) {
                wuliaoshengoudanhao = "";
            } else {
                wuliaoshengoudanhao = getdanhao;
            }
            var beizhu = $("#beizhu").val().length == 0 ? "" : $("#beizhu").val();
            var shangbaozhuangtai = $("#shangbaozhuangtai").combobox("getValue").length == 0 ? "" : $("#shangbaozhuangtai").combobox("getValue");
            var shenhezhuangtai = $("#shenhezhuangtai").combobox("getValue").length == 0 ? "" : $("#shenhezhuangtai").combobox("getValue");
            var candelete1 = $("#guanlian").combobox("getValue") == '' ? '' : $("#guanlian").combobox("getValue");

            console.log("beizhu:" + beizhu)
            console.log("shangbaozhuangtai:" + shangbaozhuangtai);
            $("#dg").datagrid({
                url: '/admin/shengoudan/getSelective',
                queryParams: {
                    wuliaoshengoudanhao: wuliaoshengoudanhao,
                    xiangmumingcheng: xiangmumingcheng,
                    shangbaozhuangtai: shangbaozhuangtai,
                    shenhezhuangtai: shenhezhuangtai,
                    beizhu: beizhu,
                    candelete: candelete1,
                    // state:$("#s_state").combobox("getValue"),
                    s_btime: $("#s_btime").datebox("getValue"),
                    s_etime: $("#s_etime").datebox("getValue")
                },
                onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    $(this).datagrid("clearSelections"); //取消所有选中项
                    $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                    $('#grid-data').menu('show', {
                        left: e.pageX,//在鼠标点击处显示菜单
                        top: e.pageY
                    });
                    e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                },
            });
        }

        //更改申购单zong
        function changeShengoudan() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择要删除的申购单！");
                return;
            }
            if(selectedRows[0].candelete == '1' || selectedRows[0].shenhezhuangtai == '已审核'){
                $.messager.alert("系统提示", "已审核或已关联的数据无法删除！");
                return;
            }
            if(selectedRows[0].shenhezhuangtai == '已审核'){
                $.messager.alert("系统提示", "该单据已审核,无法删除！");
                return;
            }
            if(selectedRows[0].candelete == '1'){
                $.messager.alert("系统提示", "该单据已关联,无法删除！");
                return;
            }
            // var id=selectedRows[0].id;
            var ids = new Array();
            if (selectedRows.length > 1) {
                for (var i = 0; i < selectedRows.length; i++) {
                    ids.push(selectedRows[i].id);
                }
            } else if (selectedRows.length == 1) {
                var id = selectedRows[0].id;
                ids.push(id);
            }
            $.messager.confirm("系统提示", "<font color=red>删除申购单后,将一同删除其下的申购详细,将无法恢复,是否删除?</font>", function (r) {
                if (r) {
                    $.post("/admin/shengoudan/delete", {'ids': ids}, function (result) {
                        if (result.success) {
                            $("#dg").datagrid("reload");
                            $("#dg2").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    }, "json");
                }
            });
        }

        //更改申购单明细
        function changeShengoudanDetails() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择要删除的申购单！");
                return;
            }
            if(selectedRows[0].candelete == '1' || selectedRows[0].shenhezhuangtai == '已审核'){
                $.messager.alert("系统提示", "已审核或已关联的数据无法删除！");
                return;
            }
            if(selectedRows[0].shenhezhuangtai == '已审核'){
                $.messager.alert("系统提示", "该单据已审核,无法删除！");
                return;
            }
            if(selectedRows[0].candelete == '1'){
                $.messager.alert("系统提示", "该单据已关联,无法删除！");
                return;
            }
            // var id=selectedRows[0].id;
            var ids = new Array();
            if (selectedRows.length > 1) {
                for (var i = 0; i < selectedRows.length; i++) {
                    ids.push(selectedRows[i].id);
                }
            } else if (selectedRows.length == 1) {
                var id = selectedRows[0].id;
                ids.push(id);
            }
            $.messager.confirm("系统提示", "<font color=red>删除申购单后,将一同删除其下的申购详细,将无法恢复,是否删除?</font>", function (r) {
                if (r) {
                    $.post("/admin/shengoudan/delete", {'ids': ids}, function (result) {
                        if (result.success) {
                            $("#dg").datagrid("reload");
                            $("#dg2").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    }, "json");
                }
            });
        }

        //加载物料数据表格
        function loadWuliaoDataGrid() {
            $("#dg3").datagrid({
                //     // 双击事件
                //之后onLoadSuccess加载这两种方法
                //悬停提示tooltip
                onLoadSuccess: function (data) {
                    $(this).datagrid('tooltip');
                    $(this).datagrid('fixRownumber');
                },
                onDblClickRow: function (index, row) {
                    openGoodsChooseDialog(); //打开物料申购窗口(填写物料申请数量)
                },
                onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    $(this).datagrid("clearSelections"); //取消所有选中项
                    $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                    $('#grid-data').menu('show', {
                        left: e.pageX,//在鼠标点击处显示菜单
                        top: e.pageY
                    });
                    e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                },
            });
        }

        function resetValue() {
            $("#num").numberbox("setValue", "");
            $("#price").val("");
        }

        //打开物料选择的大窗口
        function openShengouDetailsAddDialog() {
            $("#dlg").dialog("open").dialog("setTitle", "物料选择");
            loadWuliaoDataGrid();
            $("#tree").tree({
                url: '/admin/wuliaoTree/loadWuliaoTree',
                onLoadSuccess: function (node, data) {
                    var rootNode = $("#tree").tree('getRoot');
                    $("#tree").tree('expand', rootNode.target);
                },
                //单击某类别 展示不同详细和按钮选择
                onClick: function (node) {
                    if (node.attributes.state == 2) { // 假如是二级菜单,可以删除
                        $("#dg3").datagrid('load', {
                            "w_id": node.id
                        });
                    } else if (node.attributes.state == 1) {
                        $("#dg3").datagrid('load', {
                            "o_id": node.id
                        });
                    }
                }
            });
        }

        function openGoodsChooseDialog() {
            $("#saveAndAddNextBuuton").show();
            var selectedRows = $("#dg3").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个物料！");
                return;
            }
            var row = selectedRows[0];
            $("#action").val("add");
            $("#dlg4").dialog("open").dialog("setTitle", "申购物料");
            $("#fm4").form("load", row);
            $("#lastReturnPrice").val("￥" + row.lastPurchasingPrice);
            $("#price").numberbox("setValue", row.lastPurchasingPrice);
            $("#num").focus();
        }

        // 打开更改物料窗口
        function openShengouDetailsModifyDialog() {
            $("#saveAndAddNextBuuton").hide();
            var selectedRows = $("#dg2").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个物料！");
                return;
            }
            var row = selectedRows[0];
            $("#dlg4").dialog("open").dialog("setTitle", "修改申购单物料");
            $("#fm4").form("load", row);
            $("#lastReturnPrice").val("￥" + row.lastPurchasingPrice);
            $("#price").numberbox("setValue", row.price);
            $("#num").numberbox("setValue", row.num);
            $("#num").focus();
            $("#action").val("modify");
            $("#rowIndex").val($("#dg2").datagrid("getRowIndex", row));
        }

        function saveGoods(type) {
            var action = $("#action").val();
            if (!$("#fm5").form("validate")) {
                return;
            }
            if (action == "add") {
                var selectedRows = $("#dg3").datagrid("getSelections");
                var row = selectedRows[0];
                // var price = $("#price").numberbox("getValue");
                var number = $("#number").numberbox("getValue");
                var jiaohuodidian = $("#jiaohuodidian").val();
                var zhiliangyaoqiu = $("#zhiliangyaoqiu").val();
                var xuqiuriqi = $("#xuqiuriqi").datebox("getValue");
                var beizhu_sg = $("#beizhu_sg").val();
                var beizhu_final = "";
                if(beizhu_sg){
                    beizhu_final = beizhu_sg;
                }else{
                    beizhu_final = row.beizhu;
                }
                // var total = price * num;
                $('#dg').datagrid('appendRow', {
                    id: row.id,
                    dalei: row.dalei,
                    wuliaobianma: row.wuliaobianma,
                    mingchengguige: row.mingchengguige,
                    jiagongyigong: row.jiagongyigong,
                    danwei: row.danwei,
                    number: number,
                    kehuishou: row.kehuishou,
                    huishouxiang: row.huishouxiang,
                    beizhu: beizhu_final,
                    founder: row.founder,
                    createtime: row.createtime,
                    jiaohuodidian: jiaohuodidian,
                    zhiliangyaoqiu: zhiliangyaoqiu,
                    xuqiuriqi: xuqiuriqi
                    // total: total,
                    // typeId: row.type.id,
                    // goodsId: row.id,
                    // inventoryQuantity: row.inventoryQuantity,
                    // lastPurchasingPrice: row.lastPurchasingPrice
                });
            } else if (action == "modify") {
                var selectedRows = $("#dg2").datagrid("getSelections");
                var rowIndex=$("#rowIndex").val();
                var row = selectedRows[0];
                // var price = $("#price").numberbox("getValue");
                var number = $("#number").numberbox("getValue");
                var jiaohuodidian = $("#jiaohuodidian").val();
                var zhiliangyaoqiu = $("#zhiliangyaoqiu").val();
                var xuqiuriqi = $("#xuqiuriqi").datebox("getValue");
                var beizhu_sg = $("#beizhu_sg").val();
                var beizhu_final = "";
                if(beizhu_sg){
                    beizhu_final = beizhu_sg;
                }else{
                    beizhu_final = row.beizhu;
                }
                $('#dg2').datagrid('updateRow', {
                    index: rowIndex,
                    row: {
                        dalei: row.dalei,
                        wuliaobianma: row.wuliaobianma,
                        mingchengguige: row.mingchengguige,
                        jiagongyigong: row.jiagongyigong,
                        danwei: row.danwei,
                        number: number,
                        kehuishou: row.kehuishou,
                        huishouxiang: row.huishouxiang,
                        beizhu: beizhu_final,
                        founder: row.founder,
                        createtime: row.createtime,
                        jiaohuodidian: jiaohuodidian,
                        zhiliangyaoqiu: zhiliangyaoqiu,
                        xuqiuriqi: xuqiuriqi
                    }
                });
            }
            // setReturnListAmount();
            if (type == 1) {
                closeGoodsDialog();
            }
            closeGoodsChooseDialog();
        }

        function closeGoodsDialog() {
            $("#s_codeOrName").val("");
            $("#dlg").dialog("close");
        }

        function closeGoodsChooseDialog() {
            $("#dlg4").dialog("close");
            resetValue();
        }


        //删除申购单
        function deleteShengoudan() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择要删除的申购单！");
                return;
            }
            if(selectedRows[0].candelete == '1' || selectedRows[0].shenhezhuangtai == '已审核'){
                $.messager.alert("系统提示", "已审核或已关联的数据无法删除！");
                return;
            }
            if(selectedRows[0].shenhezhuangtai == '已审核'){
                $.messager.alert("系统提示", "该单据已审核,无法删除！");
                return;
            }
            if(selectedRows[0].candelete == '1'){
                $.messager.alert("系统提示", "该单据已关联,无法删除！");
                return;
            }
            // var id=selectedRows[0].id;
            var ids = new Array();
            if (selectedRows.length > 1) {
                for (var i = 0; i < selectedRows.length; i++) {
                    ids.push(selectedRows[i].id);
                }
            } else if (selectedRows.length == 1) {
                var id = selectedRows[0].id;
                ids.push(id);
            }
            $.messager.confirm("系统提示", "<font color=red>删除申购单后,将一同删除其下的申购详细,将无法恢复,是否删除?</font>", function (r) {
                if (r) {
                    $.post("/admin/shengoudan/delete", {'ids': ids}, function (result) {
                        if (result.success) {
                            $("#dg").datagrid("reload");
                            $("#dg2").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    }, "json");
                }
            });
        }

        function changeShangbaoState() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择申购单！");
                return;
            }
            var shangbaozhuangtai = selectedRows[0].shangbaozhuangtai;
            var id = selectedRows[0].id;
            $.messager.confirm("系统提示", "<font color=red>上报状态为" + shangbaozhuangtai + ",确定更改现在的状态吗?</font>", function (r) {
                if (shangbaozhuangtai == undefined || shangbaozhuangtai == "" || shangbaozhuangtai == null || shangbaozhuangtai == "未上报") {
                    shangbaozhuangtai = "已上报";
                } else if (shangbaozhuangtai == "已上报") {
                    shangbaozhuangtai = "未上报";
                }
                if (r) {
                    $.post("/admin/shengoudan/update", {
                        'id': id,
                        'shangbaozhuangtai': shangbaozhuangtai
                    }, function (result) {
                        if (result.success) {
                            $("#dg").datagrid("reload");
                            $("#dg2").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    }, "json");
                }
            })
        }
        function changeShenheState() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择申购单！");
                return;
            }
            var shenhezhuangtai = selectedRows[0].shenhezhuangtai;
            var id = selectedRows[0].id;
            $.messager.confirm("系统提示", "<font color=red>审核状态为" + shenhezhuangtai + ",确定更改现在的状态吗?</font>", function (r) {
                if (shenhezhuangtai == undefined || shenhezhuangtai == "" || shenhezhuangtai == null || shenhezhuangtai == "未审核") {
                    shenhezhuangtai = "已审核";
                } else if (shenhezhuangtai == "已审核") {
                    shenhezhuangtai = "未审核";
                }
                if (r) {
                    $.post("/admin/shengoudan/update", {
                        'id': id,
                        'shenhezhuangtai': shenhezhuangtai
                    }, function (result) {
                        if (result.success) {
                            $("#dg").datagrid("reload");
                            $("#dg2").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    }, "json");
                }
            })
        }

        //关联format
        function formatGuanlian(val, row) {
            console.log('format val:'+val)
            if (row != null) {
                if (val == '1') {
                    return "已关联";
                } else if (val == '0' || val == '2') {
                    return "未关联";
                }
            }
        }

        function searchProj() {
            $("#projNameList").datagrid('load', {
                "xiangmumingcheng": $("#projSearch").val()
            });
        }
    </script>
</head>
<!--url="/admin/shengoudan/getSelective"  想要查询单据一上来就直接展示,就把url放到table参数中-->
<body class="easyui-layout" style="margin: 1px">
<div region="north" style="height: 350px;">
    <table id="dg" title="申购单据查询" class="easyui-datagrid" fit="true"
           fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
           toolbar="#tb">
        <thead>
        <tr>
            <!--展示申购单主表-->
            <th field="ukyo" align="center" width="30" checkbox="true"></th>
            <th field="id" width="30" align="center" sortable="true" hidden="hidden">id</th>
            <th field="wuliaoshengoudanhao" width="80" align="center" sortable="true">申购单单号</th>
            <th field="xiangmumingcheng" width="50" align="center" sortable="true">项目名称</th>
            <th field="shengoumingcheng" width="50" align="center" sortable="true">申购名称</th>
            <th field="shenhezhuangtai" width="50" align="center" sortable="true">审核状态</th>
            <th field="shangbaozhuangtai" width="50" align="center" sortable="true">上报状态</th>
            <th field="beizhu" width="50" align="center" sortable="true">备注</th>
            <th field="founder" width="50" align="center" sortable="true">创建人</th>
            <th field="createtime" width="70" align="center" sortable="true">创建时间</th>
            <th field="candelete" width="50" align="center" sortable="true" formatter="formatGuanlian">已关联到货单</th>
        </tr>
        </thead>
    </table>

    <div id="tb">
        <div style="padding-bottom: 5px">
            <fieldset style="border-color: #E7F0FF">
                <legend>查询设置</legend>
                &nbsp;单据号：&nbsp;
                <input type="text" id="s_danhao" name="wuliaoshengoudanhao" size="15"
                       onkeydown="if(event.keyCode==13) searchShengoudan()"/>
                &nbsp;&nbsp;&nbsp;&nbsp;项目选择：<input type="text" id="projName" name="xiangmumingcheng"
                                                    style="width: 90px;" readonly="readonly"
                                                    onclick="openProjListChoose()" required="true"/>
                <input type="hidden" id="projId"/>
                <!--&nbsp;&nbsp;&nbsp;&nbsp;入库库房：<input type="text" id="storeName" name="rukukufang" size="10" readonly="readonly" onclick="openStoreListChoose()" class="easyui-validatebox" required="true"/>-->
                <!--<input type="hidden" id="storeId"/>-->
                &nbsp;&nbsp;&nbsp;日期：&nbsp;
                <input id="s_btime" name="s_btime" class="easyui-datebox" editable=false style="width:100px">
                &nbsp;&nbsp;-&nbsp;&nbsp;
                <input id="s_etime" name="s_etime" class="easyui-datebox" editable=false style="width:100px">
                <br>
                &nbsp;备&nbsp;&nbsp;&nbsp;注：&nbsp;
                <input type="text" id="beizhu" name="beizhu" size="15"
                       onkeydown="if(event.keyCode==13) searchShengoudan()"/>

                &nbsp;&nbsp;&nbsp;&nbsp;审核状态：<select class="easyui-combobox" id="shenhezhuangtai"
                                                     name="shenhezhuangtai"
                                                     style="width: 160px;" editable="false" panelHeight="auto">
                <option value="">未选择</option>
                <option value="已审核">已审核</option>
                <option value="未审核">未审核</option>
            </select>
                &nbsp;&nbsp;&nbsp;&nbsp;上报状态：<select class="easyui-combobox" id="shangbaozhuangtai"
                                                     name="shangbaozhuangtai"
                                                     style="width: 160px;" editable="false" panelHeight="auto">
                <option value="">未选择</option>
                <option value="已上报">已上报</option>
                <option value="未上报">未上报</option>
            </select>
                &nbsp;&nbsp;&nbsp;&nbsp;关联状态：<select class="easyui-combobox" id="guanlian"
                                                     name="guanlian"
                                                     style="width: 160px;" editable="false" panelHeight="auto">
                <option value="">未选择</option>
                <option value="1">已关联</option>
                <option value="0">未关联</option>
            </select>

                <!--<input type="text" id="shangbaozhuangtai" name="shangbaozhuangtai" required=true-->
                <!--size="18" class="easyui-datebox"-->
                <!--data-options="editable:false"/>-->
                <br>
                &nbsp;&nbsp;<!--<a href="javascript:changeShengoudan()" class="easyui-linkbutton" iconCls="icon-edit"
                               plain="true">修改</a>-->
                &nbsp;&nbsp;<a href="javascript:deleteShengoudan()" class="easyui-linkbutton" iconCls="icon-remove"
                               plain="true">删除</a>
                <!--&nbsp;&nbsp;<a href="javascript:exportExcel()" class="easyui-linkbutton" iconCls="icon-save"-->
                <!--plain="true">导出申购详细</a>-->
                &nbsp;&nbsp;<a href="javascript:changeShangbaoState()" class="easyui-linkbutton" iconCls="icon-ok"
                               plain="true">变更上报状态</a>
                &nbsp;&nbsp;<a href="javascript:changeShenheState()" class="easyui-linkbutton" iconCls="icon-ok"
                               plain="true">变更审核状态</a>
                &nbsp;&nbsp;<a href="javascript:searchShengoudan()" class="easyui-linkbutton" iconCls="icon-search"
                               plain="true">搜索</a>


            </fieldset>
        </div>
    </div>
</div>
<div region="center" style="margin-top: 5px;">
    <table id="dg2" class="easyui-datagrid" fit=true pageList=[10,20,30,40,50,1000000]
           fitColumns="true" rownumbers="true" checkbox="true" pagination="true"
           toolbar="#tbzi">
        <!--&gt;-->
        <thead>
        <tr>
            <!--展示申购单详细-->
            <th field="ukyo" align="center" width="30" checkbox="true"></th>
            <th field="id" width="30" align="center" sortable="true" hidden="true">编号</th>
            <th field="dalei" width="65" align="center" sortable="true">大类</th>
            <th field="wuliaobianma" width="80" align="center" sortable="true">物料编码</th>
            <th field="mingchengguige" width="100" align="center" sortable="true">名称规格</th>
            <th field="jiagongyigong" width="50" align="center" sortable="true">甲供乙供</th>
            <th field="number" width="40" align="center" sortable="true">数量</th>
            <th field="danwei" width="50" align="center" sortable="true">单位</th>
            <th field="jiaohuodidian" width="50" align="center" sortable="true">交货地点</th>
            <th field="zhiliangyaoqiu" width="50" align="center" sortable="true">质量要求</th>
            <th field="beizhu" width="50" align="center" sortable="true">备注</th>
            <th field="xuqiuriqi" width="70" align="center" sortable="true">需求日期</th>
            <th field="createtime" width="70" align="center" sortable="true">创建时间</th>
        </tr>
        </thead>
    </table>
    <div id="tbzi">
        <div style="padding-bottom: 5px">
            <fieldset style="border-color: #E7F0FF">
                <legend>明细导出</legend>
                <a href="javascript:void(0)" id="export_excel" iconCls="icon-save" class="easyui-linkbutton"
                   plain="true">保存数据</a>
                - 点击'保存数据'导出下方明细

                &nbsp;&nbsp;<!--<a href="javascript:changeShengoudanDetails()" class="easyui-linkbutton" iconCls="icon-edit"
                               plain="true">修改明细</a>
                <a href="javascript:openShengouDetailsAddDialog()" class="easyui-linkbutton" iconCls="icon-add"
                   plain="true">添加</a>
                <a href="javascript:openShengouDetailsModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit"
                   plain="true">修改</a>-->
            </fieldset>
        </div>
    </div>
</div>



<!--添加或修改申购单详情物料信息-->
<div id="dlg4" class="easyui-dialog" style="width:550px;height:465px;padding: 5px 5px"
     closed="true" buttons="#dlg-buttons4" data-options="onClose:function(){resetValue()}">

    <fieldset style="border-color: #E7F0FF">
        <legend>物料信息</legend>
        <form id="fm4" method="post">
            <table cellspacing="8px">
                <tr>
                    <td width="60">大类：</td>
                    <td>
                        <input type="text" id="dalei" name="dalei" readonly="readonly" class="easyui-validatebox"
                               required="true" size="10"/>
                    </td>
                    <td width="80">物料编码：</td>
                    <td><input type="text" id="wuliaobianma" name="wuliaobianma" readonly="readonly"
                               class="easyui-validatebox" required="true" size="15"/></td>
                </tr>
                <tr>
                    <td width="80">名称规格：</td>
                    <td colspan="4">
                        <input type="text" id="mingchengguige" name="mingchengguige" readonly="readonly"
                               class="easyui-validatebox" required="true" size="20"/>
                    </td>
                </tr>
                <tr>
                    <td width="80">单位：</td>
                    <td>
                        <!--<input type="text" id="danwei" name="danwei" class="easyui-validatebox" required="true" size="10"  />-->
                        <select class="easyui-combobox" required="true" id="danwei" name="danwei" readonly="readonly"
                                style="width: 80px;"
                                data-options="panelHeight:'auto',editable:false,valueField:'danwei',textField:'danwei',url:'/admin/wuliaodanwei/comboList'">
                        </select>
                        <!--<a href="javascript:openChooseGoodsUnitDialog()" class="easyui-linkbutton" iconCls="icon-search"  plain="true"></a>-->
                    </td>
                </tr>
                <tr>
                    <td width="60">可回收：</td>
                    <td>
                        <select id="kehuishou" class="easyui-combobox" name="kehuishou" readonly="readonly"
                                panelHeight="auto" panelMaxHeight="200px" style="width:80px;">
                            <option value="不可回收">不可回收</option>
                            <option value="可回收">可回收</option>
                        </select>
                        <!--<input type="text" id="kehuishou" name="kehuishou" class="easyui-validatebox" required="true" size="10" />-->
                    </td>
                    <td width="60">回收项：</td>
                    <td><input type="text" id="huishouxiang" name="huishouxiang" readonly="readonly"
                               class="easyui-validatebox"  size="10"/></td>
                </tr>
                <!--<tr>-->
                <!--<td width="60">备注：</td>-->
                <!--<td>-->
                <!--<input type="text" id="beizhu" name="beizhu" readonly="readonly" class="easyui-validatebox"-->
                <!--size="10"/>-->
                <!--</td>-->
                <!--<td width="60">创建人：</td>-->
                <!--<td>-->
                <!--<input type="text" id="founder_wuliao" name="founder" readonly="readonly"-->
                <!--class="easyui-validatebox" size="10"/>-->
                <!--</td>-->
                <!--</tr>-->
            </table>
        </form>
    </fieldset>
    <div style="padding-left: 12px;padding-top: 10px">
        <form id="fm5" method="post">
            <table cellspacing="8px">
                <td><input type="hidden" id="action"/><input type="hidden" id="rowIndex"/></td>
                <tr>
                    <td>申购数量：</td>
                    <td><input type="text" id="number" name="number" class="easyui-numberbox" data-options="min:1"
                               required="true" size="10"/></td>
                    <td>交货地点：</td>
                    <td><input type="text" id="jiaohuodidian" name="jiaohuodidian" data-options="min:1"
                               required="true" size="10"/></td>
                </tr>
                <tr>
                    <td>质量要求：</td>
                    <td><input type="text" id="zhiliangyaoqiu" name="zhiliangyaoqiu" data-options="min:1"
                               required="true" size="10"/></td>
                    <td>需求日期：</td>
                    <td><input type="text" id="xuqiuriqi" name="xuqiuriqi" class="easyui-datebox"
                               data-options="editable:false"
                               required="true" size="10"/></td>
                </tr>
                <tr>
                    <td>申购备注：</td>
                    <td><input type="text" id="beizhu_sg" name="beizhu" data-options="min:1"
                               size="20"/></td>
                </tr>
            </table>
        </form>
    </div>
</div>

<div id="dlg-buttons4">
    <a id="saveAndAddNextBuuton" href="javascript:saveGoods(2)" class="easyui-linkbutton"
       iconCls="icon-ok">保存并新增下一物料</a>
    <a href="javascript:saveGoods(1)" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeGoodsChooseDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>

<!--查询项目列表-->
<div id="projList" class="easyui-dialog" style="width:800px;height:460px;"
     closed="true" data-options="onClose:function(){$('#s_codeOrProjName').val('');}">
    <div class="easyui-layout" style="width:100%;height:100%;">

        <div region="north" style="height: 50px;">
            <div style="padding-top: 8px;padding-left: 40px;">
                &nbsp;项目名称：&nbsp;<input type="text" id="projSearch" size="30"
                                        onkeydown="if(event.keyCode==13) searchProj()"/>
                <a href="javascript:searchProj()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
            </div>
        </div>
        <div region="west" style="width: 200px" title="项目分类" split="true" data-options="collapsible:false">
            <div style="overflow-x: auto; overflow-y: auto; height: 90%; width:98%;">
                <ul id="projTree" class="easyui-tree" style="padding: 10px"></ul>
            </div>
            <div style="position:absolute;bottom: 10px;left: 15px">
                <a href="javascript:saveProjChoose()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">选择</a>
                <!--<a id="del" href="javascript:deleteGoodsType()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',disabled:true">删除</a>-->
            </div>
        </div>
        <div region="center">
            <table id="projNameList" title="项目列表" class="easyui-datagrid"
                   fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
                   url="/admin/dproject/getSelective" fit="true" toolbar="#tb4">
                <thead>
                <tr>
                    <th field="id" width="30" hidden=true align="center">编号</th>
                    <th field="xiangmumingcheng" width="150" align="center">项目名称</th>
                    <th field="createtime" width="90" align="left">创建时间</th>

                </tr>
                </thead>
            </table>
            <div id="tb4">
                <div>
                    <a href="javascript:saveProjChoose()" class="easyui-linkbutton" iconCls="icon-ok"
                       plain="true">选择</a>
                </div>
            </div>
        </div>
    </div>
</div>

<!--大屏幕上右键datagrid dlg-->
<div id="grid-data" class="easyui-menu" style="width: 80px; display: none;">
    <!--<div id="grid-data-edit" href="javascript:openDetailsModifyDialog()" data-options="iconCls:'icon-edit'">修改</div>-->
    <!--<div id="grid-data-delete" href="javascript:deleteDetails()" data-options="iconCls:'icon-remove'" plain="true">删除-->
</div>
</div>
<!--二级菜单右键弹出-->
<div id="tree-2" class="easyui-menu" style="width: 80px; display: none;">
    <!--<div id="tree-2-delete" href="javascript:deleteType()" data-options="iconCls:'icon-remove'">删除二级菜单</div>-->
</div>
<!--一级菜单右键弹出-->
<div id="tree-1" class="easyui-menu" style="width: 80px; display: none;">
    <!--<div id="tree-1-add-2" href="javascript:openTypeAddDialog()" data-options="iconCls:'icon-add'">添加二级菜单</div>-->
    <!--<div id="tree-1-delete" href="javascript:deleteType()" data-options="iconCls:'icon-remove'">删除一级菜单</div>-->
</div>
<!--树形菜单右键弹出-->
<div id="tree-0" class="easyui-menu" style="width: 80px; display: none;">
    <!--<div id="tree-0-add-1" href="javascript:openTypeAddDialog(0)" data-options="iconCls:'icon-add'">添加一级菜单</div>-->
    <!--<div id="tree-0-refresh" href="javascript:reloadTree()" data-options="iconCls:'icon-reload'">重新加载类别</div>-->
</div>

<div id="exportexcelinfo" class="easyui-window" style="width:380px;padding:10px 40px" closed="true">
    <form id="oil" enctype="multipart/form-data">
        <!--<input class="easyui-filebox" id="onlinefilename" name="onlinefilename" data-options="prompt:'文件上传'"-->
        <!--style="width:100%" />-->
        导出excel名称:
        <input type="text"
               id="downloadExcelName" name="downloadExcelName" style="width:100%"/>
    </form>
    <br>

    <div data-options="region:'south',border:false" style="text-align:center;padding:5px 0 0;">
        <!--<button id="exportBtn" class="easyui-linkbutton" data-options="iconCls:'icon-ok'"-->
        <button id="exportBtn" class="easyui-linkbutton" style="width:80px">导出选中
        </button>

    </div>
    <br>
</div>


</div>

<div id="dlg" class="easyui-dialog" style="width:1200px;height:460px;"
     closed="true" data-options="onClose:function(){$('#s_codeOrName').val('');}">
    <div class="easyui-layout" style="width:100%;height:100%;">

        <div region="north" style="height: 50px;">
            <div style="padding-top: 8px;padding-left: 40px;">
                &nbsp;物料编码：&nbsp;<input type="text" id="s_wuliaobianma" size="30"
                                        onkeydown="if(event.keyCode==13) searchGoods()"/>
                &nbsp;名称规格：&nbsp;<input type="text" id="s_mingchengguige" size="30"
                                        onkeydown="if(event.keyCode==13) searchGoods()"/>
                <a href="javascript:searchGoods()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
            </div>
        </div>
        <div region="west" style="width: 200px" title="物料类别" split="true" data-options="collapsible:false">
            <div style="overflow-x: auto; overflow-y: auto; height: 90%; width:98%;">
                <ul id="tree" class="easyui-tree" style="padding: 10px"></ul>
            </div>

        </div>
        <div region="center">
            <table id="dg3" title="物料列表" class="easyui-datagrid"
                   fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
                   url="/admin/dwuliao/getSelective" fit="true" toolbar="#tb3">
                <thead>
                <tr>
                    <th field="id" align="center" sortable="true" hidden="hidden">编号</th>
                    <th field="dalei"  width=10% align="center" sortable="true">大类</th>
                    <th field="wuliaobianma" width=10% align="center" sortable="true">物料编码</th>
                    <th field="mingchengguige" width=20% align="center" sortable="true">名称规格</th>
                    <th field="danwei" width=10% align="center" sortable="true">单位</th>
                    <th field="kehuishou" width=10% align="center" sortable="true">可回收</th>
                    <th field="huishouxiang" width=10% align="center" sortable="true">回收项</th>
                    <th field="beizhu" width=10% align="center" sortable="true">备注</th>
                    <th field="founder" width=10% align="center" sortable="true">创建人</th>
                    <th field="createtime" width=10% align="center" sortable="true">创建时间</th>
                </tr>
                </thead>
            </table>
            <div id="tb3">
                <div>
                    <a href="javascript:openGoodsChooseDialog()" class="easyui-linkbutton" iconCls="icon-ok"
                       plain="true">选择</a>
                </div>
            </div>
        </div>
    </div>
</div>


</body>
<script>
    //导出excel
    function exportExcel() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length == 0) {
            $.messager.alert("系统提示", "请选择要导出的申购单！");
            return;
        }
        // var node = $('#tree').tree('getSelected'); // 获取选中节点
        // var selectedRows = $("#dg").datagrid("getSelections");
        // if (selectedRows.length == 0) {
        //     $.messager.alert("系统提示", "请选择要导出的数据！");
        //     return;
        // }
        $('#exportexcelinfo').window('open').dialog('setTitle', '数据导出');
    }


    function exportExcelByZid() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length == 0) {
            $.messager.alert("系统提示", "请选择要导出的申购单！");
            return;
        }
        var id = selectedRows[0].id;

        if ($("#downloadExcelName").val().length == 0) {
            $.messager.alert("系统提示", "请设定要导出的excel文件名！");
            return;
        }
        $.ajax({
            url: '/excel/exportShengouDetailsExcel/shengouxx',
            type: 'POST',
            data: {'downloadExcelName': $("#downloadExcelName").val(), 'zongzhangid': id},
            success: function (data) {
                if (data > 0) {
                    $.messager.alert("系统提示", "操作成功！");
                    $("#dg").datagrid('reload');
                    $("#uploadonlineinfo").dialog("close");
                } else {
                    $.messager.alert("系统提示", "操作失败！");
                    $("#dg").datagrid('reload');
                    $("#uploadonlineinfo").dialog("close");
                }
            }
        });

    }

    // 导出文件byId
    $("#exportBtn").click(exportExcelByZid);

</script>
<script>
    // 导出excel按钮
    $("#export_excel").click(exportExcel);

    // 导出excel
    function exportExcel() {
        //获得选中记录 多选
        var row = $('#dg2').datagrid('getRows');
        // var row = $('#dg').datagrid('getSelections');
        // 去除不需要导出的实体字段
        var deleteKey = ['zhuanye', 'daohuoshuliang', 'updatetime',
            'zongzhangid', 'sort', 'order'
        ];
        for (var i in row) {
            for (var j in deleteKey) {
                delete row[i][deleteKey[j]];
            }
        }
        if (row) {
            $.messager.confirm('确认按钮', '你确定要导出本页申购明细数据?', function (r) {
                if (r) {
                    downloadExl(row);
                }
            })
        }
    }

    function downloadExl(data, type) {
        var keys = Object.keys(data[0]);
        var firstRow = {};
        keys.forEach(function (item) {
            console.log("item:" + item);
            firstRow[item] = item;
        });
        data.unshift(firstRow);

        var content = {};

        //把json格式的数据转换为excel的行列形式
        var sheetsData = data.map(function (item, rowIndex) {
            return keys.map(function (key, columnIndex) {
                return Object.assign({}, {
                    value: item[key],
                    position: (columnIndex > 25 ? getCharCol(columnIndex) : String.fromCharCode(65 + columnIndex)) + (rowIndex + 1),
                });
            });
        }).reduce(function (prev, next) {
            return prev.concat(next);
        });

        sheetsData.forEach(function (item, index) {
            // content[item.position] = {v: item.value};
            if (item.value == 'id') {
                content[item.position] = {v: '序列号'};
            } else if (item.value == 'dalei') {
                content[item.position] = {v: '大类'};
            } else if (item.value == 'wuliaobianma') {
                content[item.position] = {v: '物料编码'};
            } else if (item.value == 'mingchengguige') {
                content[item.position] = {v: '名称规格'};
            } else if (item.value == 'jiagongyigong') {
                content[item.position] = {v: '甲供乙供'};
            } else if (item.value == 'number') {
                content[item.position] = {v: '数量'};
            } else if (item.value == 'danwei') {
                content[item.position] = {v: '单位'};
            } else if (item.value == 'jiaohuodidian') {
                content[item.position] = {v: '交货地点'};
            } else if (item.value == 'zhiliangyaoqiu') {
                content[item.position] = {v: '质量要求'};
            } else if (item.value == 'beizhu') {
                content[item.position] = {v: '备注'};
            } else if (item.value == 'xuqiuriqi') {
                content[item.position] = {v: '需求日期'};
            } else if (item.value == 'createtime') {
                content[item.position] = {v: '创建时间'};
            } else {
                if (item.value == null || item.value == undefined || item.value == 'null') {
                    content[item.position] = {v: ''};  //remember
                } else {
                    content[item.position] = {v: item.value};  //remember
                }
            }
        });

        //设置区域,比如表格从A1到D10,SheetNames:标题
        var coordinate = Object.keys(content);
        var workBook = {
            SheetNames: ["1"],
            Sheets: {
                "1": Object.assign({}, content, {"!ref": coordinate[0] + ":" + coordinate[coordinate.length - 1]}),
            }
        };

        //这里的数据是用来定义导出的格式类型
        var excelData = XLSX.write(workBook, {bookType: "xlsx", bookSST: false, type: "binary"});
        var blob = new Blob([string2ArrayBuffer(excelData)], {type: ""});

        // filename = "石油焦";
        $.messager.prompt("输入框", "请输入保存文件名.", function (filename) {
            if (filename) {
                var nowTime = new Date().toLocaleDateString();
                saveAs(blob, filename + "-申购明细" + nowTime + ".xlsx"); //文件名
            }
        });
    }

    //字符串转字符流
    function string2ArrayBuffer(s) {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) {
            view[i] = s.charCodeAt(i) & 0xFF;
        }
        return buf;
    }

    //将指定的自然数转换为26进制表示. 映射关系: [0-25] -> [A-Z]
    function getCharCol(n) {
        var temCol = "",
            s = "",
            m = 0;
        while (n > 0) {
            m = n % 26 + 1;
            s = String.fromCharCode(m + 64) + s;
            n = (n - m) / 26;
        }
        return s;
    }

</script>
</html>